﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class uicontrol_dnChiTieuKyThuat : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Reset_Form();
    }
    protected void Reset_Form()
    {
        txtTenChiTieu.Text = "";
        txtGhiChu.Text = "";

        btnNew.Enabled = true;
        btnEdit.Visible = false;
        btnNew.Visible = true;

        Grv_LoadData();
    }
    protected void Grv_LoadData()
    {
        dsCT.DataBind();
        grvCT.DataBind();
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        btnNew.Enabled = false;
        dsCT_Act.Insert();
        Grv_LoadData();
    }
    protected void grvCT_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow))
        {
            e.Row.Cells[0].Visible = false;
        }
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow))
        {
            e.Row.Cells[2].Visible = false;
        }
        //Thêm Comfirm Message Box vào cho nút xóa
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton ibDel = (ImageButton)e.Row.FindControl("delete");
            ibDel.Attributes.Add("onclick", "javascript:return confirm('Bạn muốn xóa Chỉ tiêu " + DataBinder.Eval(e.Row.DataItem, "TenChiTieu") + "?')");
        }
    }
    protected void grvCT_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Sua")
        {
            //dsCT_Act.SelectParameters.Add("MaChiTieu", e.CommandArgument);
            Session["MaChiTieu"] = e.CommandArgument;
            Bind_EditData();

            btnNew.Visible = false;
            btnEdit.Visible = true;
            
        }
        if (e.CommandName == "Xoa")
        {
            dsCT_Act.DeleteParameters.Add("MaChiTieu", e.CommandArgument.ToString());
            dsCT_Act.Delete();

            Grv_LoadData();
        }
    }
    protected void Bind_EditData()
    {
        DataTable dt = ((DataView)dsCT_Act.Select(DataSourceSelectArguments.Empty)).Table;
        txtTenChiTieu.Text = dt.Rows[0]["TenChiTieu"].ToString();
        txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();
        ddlHD.SelectedValue = dt.Rows[0]["MaHoiDong"].ToString();

        dt.Dispose();
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        dsCT_Act.Update();
        Grv_LoadData();
    }
}
